package com.pingan.generator.api.entity.util;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.pingan.generator.api.annotation.ModaInput;
import com.pingan.generator.api.annotation.ModaOutput;
import com.pingan.generator.api.annotation.ModelFiledRemark;
import com.pingan.generator.api.util.Constains;

// 获取子字段
public class GetFile {

	public static Map<String, Object> getFileds(Class<?> cla, Map<String, Object> returnMap, String classType, String type, String url) {

		Map<String, Object> childMap = new HashMap<String, Object>();
		childMap.put("colReqOrResp", type);
		
		try {
			Class<?> classz = null;
			Field[] fields = cla.getDeclaredFields();// 对象包含的声明的字段
			if (classType != null && !classType.equals("")) {
				classz = Class.forName(classType);
			}
			for (int i = 0; i < fields.length; i++) {
				fields[i].setAccessible(true);
				if (fields[i].getGenericType() instanceof ParameterizedType) {
					if (!fields[i].getType().getName().contains("int") && !fields[i].getType().getName().contains("sun")) {
						ParameterizedType pt = (ParameterizedType) fields[i].getGenericType();
						if (pt.getRawType().equals(List.class)) {
							List<Map<String, Object>> list = new ArrayList<>();
							// 判断泛型类的类型
							if (pt.getActualTypeArguments()[0].equals(String.class)) {
								System.out.println(fields[i].getName() + " is List<String>;");
							} else if (pt.getActualTypeArguments()[0].equals(int.class) || pt.getActualTypeArguments()[0].equals(Integer.class)) {
								System.out.println(fields[i].getName() + " is List<int>;");
							}
							if(classz == null){
								classType = pt.toString().substring(pt.toString().indexOf("<") + 1, pt.toString().lastIndexOf(">"));
								classz = Class.forName(classType);
							}

							// 是泛型
							if (classz != null) {// 是泛型
								Map<String, Object> childFiledMap = getFileds(classz, childMap, classType, type, url);
								list.add(childFiledMap);
							}
							childMap.put(fields[i].getName(), list);
						} else {
							if (pt.getRawType().equals(Map.class)) {
								// 判断泛型类的类型
								if (classz != null) {
									getFileds(classz, childMap, classType, type, url);
									childMap.put(fields[i].getName(), getColJson(fields, i, type, url));
								}
							} else {
								if (classz != null) {
									getFileds(classz, childMap, classType, type, url);
									childMap.put(fields[i].getName(), getColJson(fields, i, type, url));
								}
							}
						}
					}
				} else {
					if (!fields[i].getType().getName().contains("java") && !fields[i].getType().getName().contains("int") && !fields[i].getType().getName().contains("sun")) {
						if ("int,String,boolean,double,long,byte,char".indexOf(fields[i].getGenericType().getTypeName()) == -1 && "int[]".indexOf(fields[i].getGenericType().getTypeName()) == -1) {
							Map<String,Object> FileDto = getFileds(Class.forName(fields[i].getGenericType().getTypeName()),childMap, null,type,url);
							childMap.put(fields[i].getName(), FileDto);
							childMap.put("", FileDto.get(FileDto.size()));
							continue;
						}
					}
					childMap.put(fields[i].getName(), getColJson(fields, i, type, url));
					continue;
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
			System.out.println("异常");
		}
		return childMap;
	}

	/**
	 * 
	 * @param fields
	 * @param i
	 * @param type
	 *            1-入参 2-出参
	 * @return
	 * @author xuwei
	 * @param url
	 *            controller url
	 * @date 2018年7月26日上午9:34:14
	 */
	private static String getColJson(Field[] fields, int i, String type, String url) throws Exception {
		ModaInput input = null;
		ModaOutput output = null;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (Constains.isReqOrResp.IS_REQ.equals(type)) {// 入参
			input = fields[i].getDeclaredAnnotation(ModaInput.class);
			if (input != null) {
				List<String> urlList = Arrays.asList(input.value());
				here: for (String string : urlList) {
					if (string.equals(url)) {
						resultMap.put("isNull", "Y");
						break here;
					}
				}
			} else {
				resultMap.put("isNull", "N");
			}
		} else if (Constains.isReqOrResp.IS_RESP.equals(type)) {// 出参
			output = fields[i].getDeclaredAnnotation(ModaOutput.class);
			if (output != null) {
				List<String> urlList = Arrays.asList(output.value());
				here: for (String string : urlList) {
					if (string.equals(url)) {
						resultMap.put("isNull", "Y");
						break here;
					}
				}
			} else {
				resultMap.put("isNull", "N");
			}
		} else {
			throw new Exception("出入参类型错误！");
		}
		ModelFiledRemark myFieldAnnotation = fields[i].getAnnotation(ModelFiledRemark.class);
		resultMap.put("colName", myFieldAnnotation == null ? null : myFieldAnnotation.value());
		resultMap.put("isObject", "N");
		resultMap.put("isList", "N");
		resultMap.put("attr", fields[i].getType().getName());
		resultMap.put("col", fields[i].getName());
		resultMap.put("reqOrResp", type);
		return JSON.toJSONString(resultMap);
	}
	
}
